''' @copyright:Copyright (c) 2006-2007, PreFab Software Inc. '''

import unittest, sys

import blogcosm.django_tst_setup

from blogmaker.util import is_safe_html, strip_domain, strip_domain_and_rest, strip_tlds, strip_url

class util_tests(unittest.TestCase):
    
    def test_is_safe_html(self):
        self.assert_(is_safe_html('This is a test'))
        self.assert_(is_safe_html('This is & test'))
        self.assert_(is_safe_html('This is a <b>test</b>'))
        self.assert_(is_safe_html('This is a <a href="http://example.com">test</a>'))
        self.assert_(is_safe_html('''This is a <a href='http://example.com'>test</a>'''))
        self.assert_(is_safe_html('This is a <a href=http://example.com>test</a>'))
        
        self.failIf(is_safe_html('''test <b>bold</b> and <strong>strong</strong> and 
            <strike>strike</strike> and <span>plain span</span> and 
            <span onMouseover="alert('This is a Javascript Alert')">span with script</span> and 
            <a onMouseover="alert('This is a Javascript Alert')"
            href="http://vort.org">anchor with script</a>'''))
        
        self.failIf(is_safe_html('''<script/>Test'''))
        
         
    def test_strip_url(self):
        self.assertEquals('example.com', strip_url('example.com'))
        self.assertEquals('example.com', strip_url('www.example.com'))
        self.assertEquals('example.com', strip_url('www2.example.com'))

        self.assertEquals('ww.example.com', strip_url('ww.example.com'))
        self.assertEquals('wwww.example.com', strip_url('wwww.example.com'))
        self.assertEquals('wwwexample.com', strip_url('wwwexample.com'))
        
        self.assertEquals('example.com', strip_url('http://example.com'))
        self.assertEquals('example.com', strip_url('http://example.com/'))
        self.assertEquals('example.com', strip_url('http://example.com./'))
        self.assertEquals('example.com', strip_url('http://www.example.com'))
        self.assertEquals('example.com', strip_url('http://www2.example.com'))
        
        self.assertEquals('example.com/foo/bar?baz=boo', strip_url('http://example.com/foo/bar?baz=boo'))
        self.assertEquals('example.com/foo/', strip_url('http://example.com/foo/'))
        self.assertEquals('example.com/foo/bar', strip_url('http://example.com/foo/bar'))
        self.assertEquals('example.com/foo/bar.html', strip_url('http://example.com/foo/bar.html'))
        
        self.assertEquals('example.com', strip_url('https://example.com'))
        self.assertEquals('example.com', strip_url('https://www.example.com'))
        self.assertEquals('example.com', strip_url('https://www2.example.com'))

        self.assertEquals('foo.example.com', strip_url('foo.example.com'))
        self.assertEquals('foo.example.com', strip_url('www.foo.example.com'))
        self.assertEquals('foo.example.com', strip_url('foo.example.com'))
        self.assertEquals('foo.example.com', strip_url('http://foo.example.com'))
        self.assertEquals('foo.example.com', strip_url('http://www2.foo.example.com'))
        self.assertEquals('foo.example.com', strip_url('https://foo.example.com'))

    def test_strip_domain(self):
        self.assertEquals('example.com', strip_domain('example.com'))
        self.assertEquals('example.com', strip_domain('www.example.com'))
        self.assertEquals('example.com', strip_domain('www2.example.com'))

        self.assertEquals('ww.example.com', strip_domain('ww.example.com'))
        self.assertEquals('wwww.example.com', strip_domain('wwww.example.com'))
        self.assertEquals('wwwexample.com', strip_domain('wwwexample.com'))
        
        self.assertEquals('example.com', strip_domain('http://example.com'))
        self.assertEquals('example.com', strip_domain('http://example.com/'))
        self.assertEquals('example.com', strip_domain('http://example.com./'))
        self.assertEquals('example.com', strip_domain('http://www.example.com'))
        self.assertEquals('example.com', strip_domain('http://www2.example.com'))
        
        self.assertEquals('example.com', strip_domain('http://example.com/foo/bar?baz=boo'))
        
        self.assertEquals('example.com', strip_domain('https://example.com'))
        self.assertEquals('example.com', strip_domain('https://www.example.com'))
        self.assertEquals('example.com', strip_domain('https://www2.example.com'))

        self.assertEquals('foo.example.com', strip_domain('foo.example.com'))
        self.assertEquals('foo.example.com', strip_domain('www.foo.example.com'))
        self.assertEquals('foo.example.com', strip_domain('foo.example.com'))
        self.assertEquals('foo.example.com', strip_domain('http://foo.example.com'))
        self.assertEquals('foo.example.com', strip_domain('http://www2.foo.example.com'))
        self.assertEquals('foo.example.com', strip_domain('https://foo.example.com'))


    def test_strip_domain_and_rest(self):
        self.assertEquals(('example.com', '/'), strip_domain_and_rest('example.com'))
        self.assertEquals(('example.com', '/foo'), strip_domain_and_rest('example.com/foo'))
        self.assertEquals(('example.com', '/foo'), strip_domain_and_rest('example.com/foo/'))
        
        
    def test_strip_tlds(self):
        self.assertEquals('example', strip_tlds('www.example.com'))
        self.assertEquals('example', strip_tlds('www.example.net'))
        self.assertEquals('example', strip_tlds('www.example.com.au'))
        self.assertEquals('example', strip_tlds('www.example.co.uk'))
        self.assertEquals('example', strip_tlds('http://www.example.co.uk/foo/'))
        
        
if __name__ == '__main__':
    unittest.main()